home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 2422 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: cs.vu.nl!sun4nl!xs4all!marketgraph!rvg
  2. From: rvg@marketgraph.xs4all.nl (Ruud van Gaal)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: C compiler problem
  5. Message-ID: <3005pbd60.alamito@marketgraph.xs4all.nl>
  6. Date: Tue, 30 Jan 96 15:05:48 CET
  7. References: <4ek3b2$a5k@nntp.novia.net>
  8. Reply-To: rvg@marketgraph.xs4all.nl
  9. X-Newsreader: Alamito Mail and News Manager (V2.0.4 for Waffle) registered to MARKETGRAPH VISUAL AUTOMATION
  10.  
  11. In <4ek3b2$a5k@nntp.novia.net> tsyslo@oasis.novia.net (Tony Syslo) wrote:
  12.  
  13. >I have a friend who is converting a CNET BBS game from ARREX to C.
  14. >Here is his request:
  15. >
  16. >Can anyone tell me why this program is not compiling?
  17. >I am using SAS/C 6.50, and am getting a BPTR error on line 35 with the
  18. >"struct FileHandle *".
  19.  
  20. OK, seeing that you use AREXX more than C, here are the complete docs:
  21.  
  22. >/* File Open */
  23. >
  24. >#include <clib/dos_protos.h>
  25. >#include <dos/dos.h>
  26. >#include <stdio.h>
  27. >#include <exec/types.h>
  28. >
  29. >int main();
  30.  
  31. Can be left out.
  32.  
  33. >
  34. >  char name[80];
  35. >  int age;
  36. >
  37. >int main()
  38.  
  39. void main(void) is better prototyped in C. C++ might use void main()
  40.  
  41. >{
  42. >  struct FileHandle *file_handle;
  43.  
  44.    BPTR file_handle;
  45.  
  46. >  long bytes_written;
  47. >  long bytes_read;
  48. >
  49. > printf("Enter your name: ");
  50. > scanf("%s",name);
  51. > printf("\nEnter your age: ");
  52. > scanf("%d",&age);
  53. >
  54. >
  55. >  file_handle=(struct FileHandle *)
  56. >    Open("RAM:You.dat",MODE_NEWFILE);
  57.  
  58.    file_handle=Open("RAM:You.dat",MODE_NEWFILE);    // Open returns a 
  59. BPTR
  60.  
  61. >  /* Have we opened the file successfully? */
  62. >  if(file_handle==NULL)
  63. >  {
  64. >    printf("Could not open the file!\n");
  65. >    Exit(0);
  66. >  }
  67. >  /* We have now opened a file, and are ready to start writing: */
  68. >  bytes_written=Write(file_handle,name,sizeof(name));
  69. >  bytes_written=bytes_written+Write(file_handle,age,sizeof(age));
  70.  
  71.    bytes_written=bytes_written+Write(file_handle,&age,sizeof(age));    // 
  72. Address
  73.  
  74. >  if(bytes_written!=sizeof(name)+sizeof(age))
  75. >  {
  76. >    printf("Could not save the list!\n");
  77. >    Close(file_handle);
  78. >    Exit(0);
  79. >  }
  80. >  else
  81. >    printf("Saved successfully!\n");
  82. >  printf("Memory cleared!\n");
  83.  
  84. Memory is not cleared at this point. Besides, name[1]-name[79] will still 
  85. contain the name pieces after the following:
  86.  
  87. >   name="";
  88.  
  89. You cannot assign to strings like this in C. In C, 'name' is a pointer to the 
  90. char array as defined before main(). You must do 'strcpy(name,"")' or 
  91. name[0]=0.
  92.  
  93. >   age=0;
  94. >  printf("Loading!\n");
  95. >  Seek(file_handle,0,OFFSET_BEGINNING);
  96. >  bytes_read=Read(file_handle,name,sizeof(name));
  97. >  bytes_read=bytes_read+Read(file_handle,age,sizeof(age));
  98.  
  99.    bytes_read=bytes_read+Read(file_handle,&age,sizeof(age));    // &
  100.  
  101. >  if(bytes_read!=sizeof(name)+sizeof(age))
  102. >  {
  103. >    printf("Could not read the list!\n");
  104. >    Close(file_handle);
  105. >    Exit(0);
  106. >  }
  107. >  /* Print out the data: */
  108. >    printf("Hello, %s!\",name);
  109.  
  110.      printf("Hello, %s!\n",name);    // Typo assumed; \" is chr(34); 
  111. string not ended
  112.  
  113. >    printf("You are %d years old!\n",age);
  114. >  /* Close the file: */
  115. >  Close(file_handle);
  116. >}
  117. >
  118. >Also, are there any good random number generators I could use? I am in need
  119. >of a script, or something, that is fast, for a game I am writing, or
  120. >converting, rather... Anything will work, even if it is a psuedo-random
  121. >generator.
  122.  
  123. Try srand(<n>) to set rand seek (for pseudo random streams), use 
  124. n=rand()%<mymax> to get random numbers. #include <stdlib.h>
  125.  
  126. >Thanx for any and all help!
  127. >
  128.  
  129. There you go. It compiled &ran on my machine (SAS v6.51).
  130.  
  131. --
  132. Ruud van Gaal
  133. MarketGraph Visual Automation
  134. E-Mail                : rvg@marketgraph.xs4all.nl
  135. DoomShell 4.5 homepage: http://www.xs4all.nl/~jwkorver
  136. "...Works fascinates me. I could sit and watch it for hours..."
  137.  
  138.